<template>
  <div class="tuijian-container">
    <!-- 标题区域 -->
    <div class="song-title">
      <h1>推荐歌单</h1>
      <i class="el-icon-arrow-right"></i>
    </div>

    <!-- 内容区域 -->
    <div class="song-list">
      <!-- 每日推荐歌曲 -->
      <div class="song-item" @click="getToDaySongs">
        <div class="song-img">
          <div class="mask">根据您的音乐口味生成每日更新</div>
          <img src="@/assets/images/每日歌曲推荐.jpg">
          <span><i class="el-icon-caret-right"></i></span>
        </div>
        <p>每日歌曲推荐</p>
      </div>

      <!-- 推荐歌单 -->
      <div class="song-item" v-for="item in songList" :key="item.id" @click="goToSongDetail(item)">
        <div class="song-img">
          <img :src="item.picUrl" class="play-img">
          <span><i class="el-icon-caret-right"></i></span>
          <div v-if="userInfo.userId" class="count"><i class="el-icon-caret-right"></i>{{ item.playcount >
              100000 ? (item.playcount / 10000).toFixed(0) + '万' : item.playcount
          }}</div>
          <div v-else class="count"><i class="el-icon-caret-right"></i>{{ item.playCount >
              100000 ? (item.playCount / 10000).toFixed(0) + '万' : item.playCount
          }}</div>
        </div>
        <p>{{ item.name }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/request//request'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      songList: [],
      limit: 13
    }
  },
  created() {
    this.getSongList()
  },
  watch: {
    userInfo() {
      this.getSongList()
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  methods: {
    // 获取推荐歌单列表
    async getSongList() {
      if (localStorage.getItem('token')) {
        const { data: res } = await axios.get('/recommend/resource')
        if (res.code == 200) {
          this.songList = res.recommend.slice(0, 13)
        }
      } else {
        const { data: res } = await axios.get(`/personalized?limit=${this.limit}`)
        if (res.code == 200) {
          this.songList = res.result
        }
      }
    },
    // 点击展示歌单详情
    goToSongDetail(item) {
      this.$router.push('/playlists/' + item.id)
    },

    // 获取每日歌曲推荐
    getToDaySongs() {
      this.$router.push('/home/todaymusic')
    }
  }
}
</script>

<style lang="less" scoped>
.tuijian-container {
  // position: absolute;
  // top: 225px;
  // left: 50%;
  // transform: translateX(-50%);
  width: 100%;
  // height: 550px;
  margin: 0;
  // background-color: #fff;
  // padding: 0 20px;
  color: white;

  .song-title {
    display: flex;
    align-items: center;

    // justify-content: space-between;
    h1 {
      margin: 0;
      font-size: 20px;
    }

    i {
      font-size: 20px;
      font-weight: 700;
      transform: translateY(1px);
    }
  }

  .song-list {
    display: flex;
    flex-wrap: wrap;
    // justify-content: center;
    width: 100%;
    margin-top: 10px;

    // background-color: skyblue;
    .song-item {
      position: relative;
      width: 150px;
      height: 200px;
      border-radius: 10px;
      margin-top: 10px;
      margin-right: 14px;
      // background-color: pink;
      overflow: hidden;

      &:hover .mask {
        top: 0px;
      }

      .mask {
        position: absolute;
        top: -55px;
        left: 0;
        width: 100%;
        height: 35px;
        background-color: rgba(0, 0, 0, 0.4);
        font-size: 13px;
        padding: 10px 0;
        padding-left: 5px;
        text-align: start;
        transition: all 0.5s;
        line-height: 20px;
      }

      .song-img {
        position: relative;
        width: 150px;
        height: 150px;
        border-radius: 10px;
        overflow: hidden;

        &:hover span {
          opacity: 1;
        }

        &:hover .play-img {
          transform: scale(1.2);
        }

        img {
          width: 100%;
          height: 100%;
          transition: all 0.5s;
        }

        span {
          opacity: 0;
          position: absolute;
          right: 10px;
          bottom: 10px;
          width: 30px;
          height: 30px;
          background-color: #fff;
          border-radius: 50%;
          text-align: center;
          transition: all 0.5s;

          .el-icon-caret-right {
            line-height: 30px;
            font-size: 25px;
            color: #962ed6;
          }
        }

        .count {
          position: absolute;
          top: 10px;
          right: 10px;
          font-size: 12px;
          color: #fff;
        }
      }

      p {
        margin-top: 5px;
        font-size: 14px;
        height: 40px;
        text-align: start;
        overflow: hidden;
        text-overflow: ellipsis;
        /* 3.设置盒子属性为-webkit-box  必须的 */
        display: -webkit-box;
        /* 4.-webkit-line-clamp 设置为2，表示超出2行的部分显示省略号，如果设置为3，那么就是超出3行部分显示省略号 */
        -webkit-line-clamp: 2;
        /* 5.字面意思：单词破坏：破坏英文单词的整体性，在英文单词还没有在一行完全展示时就换行  即一个单词可能会被分成两行展示 */
        word-break: break-all;
        /* 6.盒子实现多行显示的必要条件，文字是垂直展示，即文字是多行展示的情况下使用 */
        -webkit-box-orient: vertical;

        .el-icon-caret-right {
          font-size: 16px;
        }
      }
    }
  }
}
</style>